#include "backupwidget.h"
#include <ibase.h>

#define RESPBUF	1024

BackupWidget::BackupWidget()
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);

fbbackup = NULL;

QDate ldate = QDate::currentDate();
QString path_;
QString home_path = QDir::homePath();
QString desktop_path = home_path;
desktop_path.append( QDir::separator() );
desktop_path.append( QLatin1String("Desktop") );
QDir desktop_dir(desktop_path);
    if ( true == desktop_dir.exists() )
    {
    path_ = desktop_path;
    }
    else
    {
    path_ = home_path;
    }
path_.append( QDir::separator() );
path_.append( QLatin1String("bk_") );
path_.append( ldate.toString("yyyyMMdd") );
path_.append( QLatin1String(".fbk") );
path = QDir::toNativeSeparators(path_);

QObject::connect(backRunToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(run_()));
QObject::connect(backCancelToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(close_backup()));                 
QObject::connect(saveFileNameButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(setSaveFileName()));
QObject::connect(backFileSelToolButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(setDatabaseName()));
}

BackupWidget::~BackupWidget()
{
}

void BackupWidget::closeEvent(QCloseEvent * e)
{
    if ( NULL != fbbackup )
    {
        if ( true == fbbackup->isRunning() )
        {
        QMessageBox::warning(this,
                    tr("Error"),
                    tr("Backup is running") );
        e->ignore();
        }
        else
        {
        fbbackup->disconnect();
        delete fbbackup;
        fbbackup = NULL;
        e->accept();
        }
    }
    else
    {
    e->accept();
    }

}

void BackupWidget::run_()
{
    if (NULL != fbbackup)
    {
        if (true == fbbackup->isRunning() )
        {
        QMessageBox::warning(this,
                     tr("Error"),
                     tr("Backup is running") );
        return;
        }
        else
        {
        fbbackup->disconnect();
        delete fbbackup;
        fbbackup = NULL;
        }
    }
    
fbbackup = new FBBackup();

QObject::connect( fbbackup,
                  SIGNAL( log_line(QString) ),
                  this,
                  SLOT( write_log(QString) ) );
QObject::connect( fbbackup,
                  SIGNAL( finished() ),
                  this,
                  SLOT( enable_bbutton() ) );

QString back_file = saveFileNameLabel->text(); 
    if ( true == back_file.isEmpty() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("No backup file selected") );
    return;
    }

    if ( true == backEmbCheckBox->isChecked() )
    {
    QString db_file_ = QCoreApplication::applicationDirPath();
    db_file_.append( QDir::separator() );
    db_file_.append( QLatin1String("COLL0.FDB") );
    QString db_file = QDir::toNativeSeparators(db_file_);
    fbbackup->set_config( db_file,
                          back_file,
                          QLatin1String("SYSDBA"),
                          QLatin1String("masterp"),
                          QLatin1String("service_mgr"),
                          false );
    }
    else
    {
    QString serv_name;
        if (true == backServiceCheckBox->isChecked())
        {
        serv_name = backHostLineEdit->text();
        serv_name.append( QLatin1String(":") );
        serv_name.append( backServLineEdit->text() );
        }
        else
        {
        serv_name = QLatin1String("service_mgr"); 
        }
 
    QString db_file = backNameLineEdit->text();
    QString user = QLatin1String("");
    QString pass = QLatin1String("");

        if ( true == db_file.isEmpty() )
        {
        QMessageBox::warning(this,
                     tr("Error"),
                     tr("No database selected") );
        return;
        }

        if (true == backCredCheckBox->isChecked() )
        {
        user = backUsrLineEdit->text();
        pass = backPasswdLineEdit->text();
        }

    fbbackup->set_config( db_file,
                          back_file,
                          user,
                          pass,
                          serv_name,
                          false );
    }
fbbackup->start();
backRunToolButton->setDisabled(true);
backCancelToolButton->setDisabled(true);

}

void BackupWidget::write_log(QString s)
{
backLogTextBrowser->insertPlainText(s);
}

void BackupWidget::enable_bbutton()
{
    if (true != backRunToolButton->isEnabled())
    {
    backRunToolButton->setDisabled(false);
    backCancelToolButton->setDisabled(false);
    }

    if (NULL != fbbackup)
    {
    fbbackup->disconnect();
    delete fbbackup;
    fbbackup = NULL;
    }
}

void BackupWidget::close_backup()
{
    if (NULL != fbbackup)
    {
        if (true == fbbackup->isRunning() )
        {
        QMessageBox msgBox;
        msgBox.setText( tr("Abort runnung backup?!\nApplication will close.") );
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            switch (msgBox.exec())
            {
                case QMessageBox::Yes:
                      if (NULL != fbbackup)
                      {
                          if (true == fbbackup->isRunning() )
                          {
                          fbbackup->disconnect();
                          fbbackup->terminate();
                          fbbackup->exit(1);
                          }
                      }
                close();
                qApp->exit(1);
                break;

                default:
                break;
            }
        }
        else
        {
        fbbackup->disconnect();
        delete fbbackup;
        fbbackup = NULL;
        QMessageBox::information( this,
                                  tr("Information"),
                                  tr("Reconnect to DB") );        
        close();
        }
    }
    else
    {
    QMessageBox::information( this,
                              tr("Information"),
                              tr("Reconnect to DB") );    
    close();
    }
return;
}

void BackupWidget::setSaveFileName()
{
QString fileName = QFileDialog::getSaveFileName(this,
                   tr("Select backup file"),
                   path,
                   tr("Backup files (*.fbk);;All Files (*)"));
    if ( !fileName.isEmpty() )
    {
    QString native_filename = QDir::toNativeSeparators(fileName);
    saveFileNameLabel->setText(native_filename);
    }
}

void BackupWidget::setDatabaseName()
{
QString fileName = QFileDialog::getOpenFileName(this,
                   tr("Select database"),
                   QString(""),
                   tr("Database files (*.fdb);;All Files (*)"));
    if ( !fileName.isEmpty() )
    {
    backNameLineEdit->setText(fileName);
    }
}
